﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Trip_Flight_Entity : EntityBase<TRIP_FLIGHT>
    {
        public override RESP_Info Add(TRIP_FLIGHT model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into TRIP_FLIGHT(TRIP_ID,FLIGHT_ID) values (@TRIP_ID,@FLIGHT_ID)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", model.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_ID", model.FLIGHT_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加行程航班成功</p></h5>";
                    info.Url = "TripFlightList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加行程航班失败</p></h5>";
                    info.Url = "TripFlightAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加行程航班失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(TRIP_FLIGHT model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update TRIP_FLIGHT set FLIGHT_ID=@FLIGHT_ID where TRIP_ID=@TRIP_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", model.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_ID", model.FLIGHT_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑行程航班成功</p></h5>";
                    info.Url = "TripFlightList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑行程航班失败</p></h5>";
                    info.Url = "TripFlightAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑行程航班失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(TRIP_FLIGHT tripFlight)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from TRIP_FLIGHT where TRIP_ID=@TRIP_ID AND FLIGHT_ID=@FLIGHT_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", tripFlight.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("FLIGHT_ID", tripFlight.FLIGHT_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除行程航班成功</p></h5>";
                    info.Url = "TripFlightList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除行程航班失败</p></h5>";
                    info.Url = "TripFlightList.aspx";

                    Tom.Net.Log.LogManager.Error("删除行程航班失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override TRIP_FLIGHT GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select TRIP_ID,FLIGHT_ID from TRIP_FLIGHT where TRIP_ID=@TRIP_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        TRIP_FLIGHT tripFlight = new TRIP_FLIGHT();
                        if (!reader.IsDBNull(0)) tripFlight.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tripFlight.FLIGHT_ID = reader.GetString(1);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return tripFlight;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程航班数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override TRIP_FLIGHT GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        TRIP_FLIGHT tripFlight = new TRIP_FLIGHT();
                        if (!reader.IsDBNull(0)) tripFlight.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tripFlight.FLIGHT_ID = reader.GetString(1);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return tripFlight;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程航班数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<TRIP_FLIGHT> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<TRIP_FLIGHT> tripFlights = new List<TRIP_FLIGHT>();
                    while (reader.Read())
                    {
                        TRIP_FLIGHT tripFlight = new TRIP_FLIGHT();
                        if (!reader.IsDBNull(0)) tripFlight.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tripFlight.FLIGHT_ID = reader.GetString(1);

                        tripFlights.Add(tripFlight);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return tripFlights;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程航班数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
